define(["jquery", "easy-admin", "xm-select"], function ($, ea) {
    let laydate = layui.laydate
        , form = layui.form;
    var init = {
        table_elem: '#currentTable',
        table_render_id: 'currentTableRenderId',
        index_url: 'collage.collage/index',
        add_url: 'collage.collage/add',
        edit_url: 'collage.collage/edit',
        delete_url: 'collage.collage/delete',
        export_url: 'collage.collage/export',
        modify_url: 'collage.collage/modify',
    };

    var Controller = {

        index: function () {
            ea.table.render({
                init: init,
                toolbar: ['refresh',
                    [{
                        text: '添加',
                        url: init.add_url,
                        method: 'open',
                        auth: 'add',
                        class: 'layui-btn layui-btn-normal layui-btn-sm',
                        icon: 'fa fa-plus ',
                        extend: 'data-full="true"',
                    }],
                    'delete', 'export'],
                minWidth: 90,
                cols: [[
                    {type: 'checkbox'},
                    {field: 'id', title: '拼团id', width: 80},
                    {field: 'goods_id', title: '拼团商品', width: 100},
                    {field: 'goods_cate', title: '商品规格', width: 100},
                    {field: 'logo', title: '封面图', templet: ea.table.image, width: 90},
                    {field: 'stock', title: '拼团库存', width: 100},
                    {field: 'get_start_time', title: '拼团开始时间', width: 160},
                    {field: 'get_end_time', title: '拼团结束时间', width: 160},
                    {field: 'number', title: '拼团人数', width: 90},
                    {
                        field: 'status',
                        search: 'select',
                        selectList: ["禁用", "启用"],
                        title: '状态',
                        templet: ea.table.switch,
                        width: 180
                    },
                    {field: 'create_time', title: '创建时间', width: 160},
                    {
                        width: 250, title: '操作', templet: ea.table.tool, operat: [
                            [{
                                text: '编辑',
                                extra: 'name',
                                url: init.edit_url,
                                method: 'open',
                                auth: 'edit',
                                class: 'layui-btn layui-btn-xs layui-btn-success',
                                extend: 'data-full="true"',
                            }],
                            'delete']
                    },
                ]],
            });

            ea.listen();
        },
        add: function () {
            laydate.render({
                elem: '#start_time', //指定元素
                type: 'datetime'
                , range: '~'
            });
            let pay_type = xmSelect.render({
                el: '#pay_type',
                radio: false,
                toolbar: {show: true},
                name: 'pay_type',
                paging: true,
                filterable: true,
                tips: '选择支付方式',
                data: [],
            });
            //获取支付方式
            $.get("/admin/system.config/getPaymentByList").then(res => {
                if (res.code == 1) {
                    pay_type.update({
                        data: res.data
                    })
                }
            });
            let goods = xmSelect.render({
                el: '#goods_id',
                radio: true,
                clickClose: true,
                toolbar: {show: true},
                name: 'goods_id',
                paging: true,
                filterable: true,
                tips: '选择商品',
                data: [],
                on: function (data) {
                    //arr:  当前多选已选中的数据
                    var arr = data.arr;
                    //change, 此次选择变化的数据,数组
                    var change = data.change;
                    //isAdd, 此次操作是新增还是删除
                    var isAdd = data.isAdd;

                    //可以return一个数组, 代表想选中的数据
                    //return []
                    if (arr.length == 0) {
                        //进行操作
                    } else {
                        $.get("/admin/mall.goods/getGoodsBySpecs", {
                            goods_id: arr[0].value
                        }).then(res => {
                            if (res.code == 1) {
                                let arr = [];
                                if (res.data.length != 0) {
                                    res.data.forEach(function (item, index) {
                                        arr.push({
                                            name: item.get_specs,
                                            value: item.id,
                                            price: item.price,
                                            stock: item.stock
                                        })
                                    })
                                }
                                goods_cate.update({
                                    data: arr
                                })
                            }
                        });
                    }
                },

            })
            $.get("/admin/mall.goods/getGoodsByListNameInfo").then(res => {
                if (res.code == 1) {
                    goods.update({
                        data: res.data
                    });
                }
            });
            let goods_cate = xmSelect.render({
                el: '#goods_cate',
                radio: true,
                clickClose: true,
                toolbar: {show: true},
                paging: true,
                filterable: true,
                tips: '商品规格',
                name: 'goods_cate',
                data: [],
                on: function (data) {
                    //arr:  当前多选已选中的数据
                    var arr = data.arr;
                    //change, 此次选择变化的数据,数组
                    var change = data.change;
                    //isAdd, 此次操作是新增还是删除
                    var isAdd = data.isAdd;

                    if (arr.length != 0) {
                        $("#y_price").val(arr[0].price)
                        $("#y_stock").val(arr[0].stock)
                    }
                }

            })
            form.on('radio(selectNumber)', function (data) {
                // console.log(data.value); //被点击的radio的value值
                if (data.value == 0) {
                    $(".setNumber").addClass('layui-hide')
                } else {
                    $(".setNumber").removeClass('layui-hide')
                }
            });
            form.on('radio(selectAddress)', function (data) {
                // console.log(data.value); //被点击的radio的value值
                if (data.value == 0) {
                    $(".setAddress").addClass('layui-hide')
                } else {
                    $(".setAddress").removeClass('layui-hide')
                }
            });
            const province = xmSelect.render({
                el: '#province',
                autoRow: true,
                paging: true,
                filterable: true,
                toolbar: {
                    show: true,
                },
                name: 'address',
                model: {
                    label: {
                        type: 'block',
                        block: {
                            //最大显示数量, 0:不限制
                            showCount: 2,
                            //是否显示删除图标
                            showIcon: true,
                        }
                    }
                },
                tree: {
                    show: true,
                    showFolderIcon: true,
                    showLine: true,
                    indent: 20,
                    expandedKeys: [-1],
                    lazy: true,
                    icon: 'hidden',

                    load: function (item, cb) {
                        console.log(item)
                        setTimeout(function () {
                            if (item.name.endsWith('2')) {
                                return cb([]);
                            }
                            cb([
                                {name: item.name + 1, value: item.value + '1', children: []},
                                {name: item.name + 2, value: item.value + '2', children: []},
                            ])
                        }, 500)
                    }
                },
                height: '200px',
                data: []
            });
            //获取城市地址
            $.get("/admin/system.province/getProvinceCityByListTree").then(res => {
                if (res.code == 1) {
                    province.update({
                        data: res.data
                    })
                }
            });

            ea.listen();
        },
        edit: function () {
            laydate.render({
                elem: '#start_time', //指定元素
                type: 'datetime'
                , range: '~'
            });
            let goods = xmSelect.render({
                el: '#goods_id',
                radio: true,
                clickClose: true,
                toolbar: {show: true},
                name: 'goods_id',
                paging: true,
                filterable: true,
                tips: '选择商品',
                data: [],
                on: function (data) {
                    //arr:  当前多选已选中的数据
                    var arr = data.arr;
                    //change, 此次选择变化的数据,数组
                    var change = data.change;
                    //isAdd, 此次操作是新增还是删除
                    var isAdd = data.isAdd;

                    //可以return一个数组, 代表想选中的数据
                    //return []
                    if (arr.length == 0) {
                        //进行操作
                    } else {
                        $.get("/admin/mall.goods/getGoodsBySpecs", {
                            goods_id: arr[0].value
                        }).then(res => {
                            if (res.code == 1) {
                                let arr = [];
                                if (res.data.length != 0) {
                                    res.data.forEach(function (item, index) {
                                        arr.push({
                                            name: item.get_specs,
                                            value: item.id,
                                            price: item.price,
                                            stock: item.stock
                                        })
                                    })
                                }
                                goods_cate.update({
                                    data: arr
                                })
                            }
                        });
                    }
                },
            })
            $.get("/admin/mall.goods/getGoodsByListNameInfo").then(res => {
                if (res.code == 1) {
                    goods.update({
                        data: res.data
                    });
                }
            });
            goods.update({
                initValue: [goods_id]
            })
            let pay_type = xmSelect.render({
                el: '#pay_type',
                radio: false,
                toolbar: {show: true},
                name: 'pay_type',
                paging: true,
                filterable: true,
                tips: '选择支付方式',
                data: [],
            });
            //获取支付方式
            $.get("/admin/system.config/getPaymentByList").then(res => {
                if (res.code == 1) {
                    pay_type.update({
                        data: res.data
                    })
                }
            });
            let arr_pay = [];
            arr_pay = pay_type_id.split(',');
            pay_type.update({
                initValue: arr_pay
            })

            $.get("/admin/mall.goods/getGoodsBySpecs", {
                goods_id: goods_id
            }).then(res => {
                if (res.code == 1) {
                    let arr = [];
                    if (res.data.length != 0) {
                        res.data.forEach(function (item, index) {
                            arr.push({
                                name: item.get_specs,
                                value: item.id,
                                price: item.price,
                                stock: item.stock
                            })
                        })
                    }
                    goods_cate.update({
                        data: arr,
                        initValue: [goods_cate_id]
                    })
                }
            });
            let goods_cate = xmSelect.render({
                el: '#goods_cate',
                radio: true,
                clickClose: true,
                toolbar: {show: true},
                paging: true,
                filterable: true,
                tips: '商品规格',
                name: 'goods_cate',
                data: [],
                on: function (data) {
                    //arr:  当前多选已选中的数据
                    var arr = data.arr;
                    //change, 此次选择变化的数据,数组
                    var change = data.change;
                    //isAdd, 此次操作是新增还是删除
                    var isAdd = data.isAdd;

                    if (arr.length != 0) {
                        $("#y_price").val(arr[0].price)
                        $("#y_stock").val(arr[0].stock)
                    }
                }

            })
            form.on('radio(selectNumber)', function (data) {
                // console.log(data.value); //被点击的radio的value值
                if (data.value == 0) {
                    $(".setNumber").addClass('layui-hide')
                } else {
                    $(".setNumber").removeClass('layui-hide')
                }
            });
            form.on('radio(selectAddress)', function (data) {
                console.log(data.value); //被点击的radio的value值
                if (data.value == 0) {
                    $(".setAddress").addClass('layui-hide')
                } else {
                    $(".setAddress").removeClass('layui-hide')
                }
            });
            const province = xmSelect.render({
                el: '#province',
                autoRow: true,
                paging: true,
                filterable: true,
                toolbar: {
                    show: true,
                },
                name: 'address',
                model: {
                    label: {
                        type: 'block',
                        block: {
                            //最大显示数量, 0:不限制
                            showCount: 2,
                            //是否显示删除图标
                            showIcon: true,
                        }
                    }
                },
                tree: {
                    show: true,
                    showFolderIcon: true,
                    showLine: true,
                    indent: 20,
                    expandedKeys: [-1],
                    lazy: true,
                    icon: 'hidden',

                    load: function (item, cb) {
                        console.log(item)
                        setTimeout(function () {
                            if (item.name.endsWith('2')) {
                                return cb([]);
                            }
                            cb([
                                {name: item.name + 1, value: item.value + '1', children: []},
                                {name: item.name + 2, value: item.value + '2', children: []},
                            ])
                        }, 500)
                    }
                },
                height: '200px',
                data: []
            });
            let arr_address_s = [];
            arr_address_s = arr_address.split(',');

            //获取城市地址
            $.get("/admin/system.province/getProvinceCityByListTree").then(res => {
                if (res.code == 1) {
                    province.update({
                        data: res.data,
                        initValue: arr_address_s
                    })
                }
            });
            ea.listen();
        },
    };
    return Controller;
});